package cn.kxx.controller;

import cn.kxx.config.DateUtil;
import cn.kxx.constant.MessageConstant;
import cn.kxx.entity.Result;
import cn.kxx.service.MemberService;
import cn.kxx.service.ReportService;
import cn.kxx.service.SetmealService;
import cn.kxx.vo.BusinessReportVo;
import cn.kxx.vo.HotSetmealVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 会员统计分析
 *
 * @author 1136249393
 * @date 2023/03/02
 */
@Api(tags = "统计分析模块")
@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private MemberService memberService;
    @Reference
    private SetmealService setmealService;

    /**
     * @return {@link Result}
     */
    @ApiOperation("会员数量统计")
    @GetMapping("/getMemberReport")
    public Result getMemberReport() {
//        获取当前月份
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            Date time = calendar.getTime();

            list.add(new SimpleDateFormat("yyyy-MM").format(time));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("months", list);
       List<Integer> list1 = memberService.findMemberCountByMouth(list);

       map.put("memberCount",list1);
       return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);

    }

    @ApiOperation("套餐预约占比统计")
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport() {

        List<Map<String, Object>> data = setmealService.findSetmealCount();

        return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, data);
    }


    @Reference
    private ReportService reportService;


    @ApiOperation("统计运营数据数据")
    @GetMapping("/getBusinessReportData")
    public Result GetBusinessReportData() {

        BusinessReportVo businessReportVo = reportService.getBusinessReport();

        return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, businessReportVo);

    }

    @ApiOperation("execl表格的导入导出")
    @RequestMapping("exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response, HttpServletRequest request) throws IOException, InvalidFormatException {

//        获取运营数据
        BusinessReportVo businessReport = reportService.getBusinessReport();
//        获取模板位置
        File file = new File("D:\\mavenworkspace\\kxx_health\\kxx_health\\health_backend\\src\\main\\resources\\static\\template\\report_template.xlsx");

//获取工作普
        XSSFWorkbook sheets = new XSSFWorkbook(file);
//        获取第一个表格
        XSSFSheet sheetAt = sheets.getSheetAt(0);
//        获取第三行第六列
        XSSFRow row = sheetAt.getRow(2);
        XSSFCell cell = row.getCell(5);
//        把时间填充至cell内
        Date reportDate = businessReport.getReportDate();
        String dateToStr = DateUtil.dateToStr(reportDate, "yyyy-MM-dd");
        cell.setCellValue(dateToStr);

        //        获取第五行第六列
        XSSFRow row1 = sheetAt.getRow(4);
        XSSFCell cell1 = row1.getCell(5);
//        把新增会员数添加至表格
        cell1.setCellValue(businessReport.getTodayNewMember());
        //        获取第五行第8列
        XSSFRow row2 = sheetAt.getRow(4);
        XSSFCell cell2 = row2.getCell(7);
//        把新增会员数添加至表格
        cell2.setCellValue(businessReport.getTotalMember());
        //        获取第六行第六列
        XSSFRow row3 = sheetAt.getRow(5);
        XSSFCell cell3 = row3.getCell(5);
//        把本周新增会员数添加至表格
        cell3.setCellValue(businessReport.getThisWeekNewMember());
//        获取第六行第8列
        XSSFRow row4 = sheetAt.getRow(5);
        XSSFCell cell4 = row4.getCell(7);
//        把月新增会员数添加至表格
        cell4.setCellValue(businessReport.getThisMonthNewMember());


        //        获取第8行第6列
        XSSFRow row5 = sheetAt.getRow(7);
        XSSFCell cell5 = row5.getCell(5);
//        把今日预约数添加至表格
        cell5.setCellValue(businessReport.getTodayVisitsNumber());

        //        获取第8行第8列
        XSSFRow row6 = sheetAt.getRow(7);
        XSSFCell cell6 = row6.getCell(7);
//        把今日到诊数添加至表格
        cell6.setCellValue(businessReport.getTodayOrderNumber());

        //        获取第9行第6列
        XSSFRow row7 = sheetAt.getRow(8);
        XSSFCell cell7 = row7.getCell(5);
//        把今日预约数添加至表格
        cell7.setCellValue(businessReport.getThisWeekVisitsNumber());

        //        获取第9行第8列
        XSSFRow row8 = sheetAt.getRow(8);
        XSSFCell cell8 = row8.getCell(7);
//        把今日到诊数添加至表格
        cell8.setCellValue(businessReport.getThisWeekOrderNumber());


        //        获取第10行第6列
        XSSFRow row9 = sheetAt.getRow(9);
        XSSFCell cell9 = row9.getCell(5);
//        把今日预约数添加至表格
        cell9.setCellValue(businessReport.getThisMonthVisitsNumber());

        //        获取第10行第8列
        XSSFRow row10 = sheetAt.getRow(9);
        XSSFCell cell10 = row10.getCell(7);
//        把今日到诊数添加至表格
        cell10.setCellValue(businessReport.getThisMonthOrderNumber());

//获取热门套餐
        List<HotSetmealVo> hotSetmeal = businessReport.getHotSetmeal();
        for (int i = 0; i < 4; i++) {
            XSSFRow row11 = sheetAt.getRow(12 + i);
            XSSFCell cell11 = row11.getCell(4);
            XSSFCell cell12 = row11.getCell(5);
            XSSFCell cell13 = row11.getCell(6);
            XSSFCell cell14 = row11.getCell(7);
            cell11.setCellValue(hotSetmeal.get(i).getName());

            cell12.setCellValue(hotSetmeal.get(i).getSetmealCount());
            BigDecimal bigDecimal = new BigDecimal(hotSetmeal.get(i).getProportion()).setScale(2, BigDecimal.ROUND_HALF_UP);
            cell13.setCellValue(bigDecimal.toString());
            cell14.setCellValue(hotSetmeal.get(i).getRemark());
        }

        try {
            ServletOutputStream outputStream = response.getOutputStream();

//设置响应头进行下载
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
            sheets.write(outputStream);
            outputStream.flush();
            outputStream.close();
            sheets.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        return null;

    }

}
